import { codegen } from "./codegen";
import { parser } from "./parser";
import { tokenizer } from "./tokenizer";
import { transformer } from "./transformer";

export function compiler(code: string) {
  // 1. 将源代码 code 生成 tokens
  const tokens = tokenizer(code);
  // 2. 将 tokens 生成原始 ast
  const ast = parser(tokens);
  // 3. 将原始 ast 转换成目标 ast
  const targetAst = transformer(ast);
  // 4. 通过转换成的 ast 生成目标 code
  const targetCode = codegen(targetAst);

  return targetCode;
}

// Source code ----源码--> tokenizer ---tokens--> parser ----AST---> transformer ---(ast---change-->target ast)---> codegen ---target code--> compiler
